﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Activities;
using MTechApplicationServices.Data;
using MTechApplicationServices.Contracts;
using MTechApplicationServices.Application;

namespace MTechApplicationServices
{
    public partial class SelectionApproval : System.Web.UI.Page
    {
        Applicant applicant;
        int appID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetApplicant();
            }
        }

        private void SendHumanScreenComplete(bool selected)
        {
            //created proxy using svcutil, adding svc ref didn't create ApplicationServiceClient
            ApplicationServiceClient proxy = new ApplicationServiceClient();

            //Create the result
            MTechApplicationServices.Application.HumanScreeningResult result = new MTechApplicationServices.Application.HumanScreeningResult()
            {
                AppID = Convert.ToInt32(LabelAppID.Text),
                SelectionApproved = selected,
            };

            //Wrap it in a request
            SubmitInterviewResultRequest request = new SubmitInterviewResultRequest(result);

            //Call the workflow service
            proxy.SubmitInterviewResult(result);

            proxy.Close();
        }

        private string GetRedirectString(string UriFormat, params object[] args)
        {
            return Server.UrlPathEncode(string.Format(UriFormat, args));
        }

        private void GetApplicant()
        {
            appID = GetAppIDFromRequest();

            if (appID == -1)
            {
                string s = GetRedirectString("AppMessage.aspx?MsgID=ErrInvalidAppID");
                Response.Redirect(s, true);
            }
            else
            {
                using (MTechApplicationDataEntities ctx = new MTechApplicationDataEntities())
                {
                    var query = from a in ctx.Applicants
                                where a.ApplicationID == appID
                                select a;

                    applicant = query.FirstOrDefault();

                    if (applicant == null)
                        Response.Redirect(GetRedirectString("AppMessage.aspx?MsgID=ErrUnknownAppID"), true);
                    else
                    {
                        this.LabelAppID.Text = appID.ToString();
                        this.LabelName.Text = applicant.ApplicantName;
                        this.LabelEmail.Text = applicant.Email;
                        this.LabelIDNo.Text = applicant.IDNo;
                        this.LabelEducation.Text = applicant.HighestDegree;
                        this.LabelProgram.Text = applicant.Program;
                        this.LabelMediumOfInstruction.Text = applicant.MediumOfInstruction;
                        this.LabelExperience.Text = applicant.Experience.ToString();
                        this.LabelEnglishTestType.Text = applicant.EnglishTestType;
                        this.LabelEnglishTestScore.Text = applicant.EnglishTestScore.ToString();
                        this.LabelRequestID.Text = applicant.RequestID.ToString();
                    }
                }
            }
        }

        private int GetAppIDFromRequest()
        {
            string appIDArg = Request.QueryString["AppID"];
            int appId = -1;

            Int32.TryParse(appIDArg, out appId);

            return appId;           
        }

        protected void ButtonApprove_Click(object sender, EventArgs e)
        {
            SendHumanScreenComplete(true);

            // Redirect to message page
            Response.Redirect(
                GetRedirectString(
                "AppMessage.aspx?MsgID=AppIDStatusUpdated&AppID={0}&Status={1}",
                LabelAppID.Text, "Selected"),
                true);
        }

        protected void ButtonReject_Click(object sender, EventArgs e)
        {
            SendHumanScreenComplete(false);

            // Redirect to message page
            Response.Redirect(
                GetRedirectString(
                "AppMessage.aspx?MsgID=AppIDStatusUpdated&AppID={0}&Status={1}",
                LabelAppID.Text, "Selected"),
                true);
        }
    }
}